{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utility functions for MintPy-tutorial\n",
    "\n",
    "+ Requirements: `ipynb` to allow importing functions from other ipynb files. It can be installed as `pip install ipynb`.\n",
    "+ Recommended usage: `from ipynb.fs.full import utils`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import widgets, Layout, Box, GridspecLayout\n",
    "\n",
    "# Basic mcq\n",
    "def create_multiple_choice_widget(description, options, correct_answer, hint):\n",
    "    \"\"\"Create multiple choices widget.\"\"\"\n",
    "    if correct_answer not in options:\n",
    "        options.append(correct_answer)\n",
    "\n",
    "    correct_answer_index = options.index(correct_answer)\n",
    "\n",
    "    radio_options = [(words, i) for i, words in enumerate(options)]\n",
    "    alternativ = widgets.RadioButtons(\n",
    "        options = radio_options,\n",
    "        description = '',\n",
    "        disabled = False,\n",
    "        indent = False,\n",
    "        align = 'center',\n",
    "    )\n",
    "\n",
    "    description_out = widgets.Output(layout=Layout(width='auto'))\n",
    "    with description_out:\n",
    "        print(description)\n",
    "\n",
    "    feedback_out = widgets.Output()\n",
    "\n",
    "    def check_selection(b):\n",
    "        a = int(alternativ.value)\n",
    "        if a==correct_answer_index:\n",
    "            s = '\\x1b[6;30;42m' + \"correct\" + '\\x1b[0m' +\"\\n\"\n",
    "        else:\n",
    "            s = '\\x1b[5;30;41m' + \"try again\" + '\\x1b[0m' +\"\\n\"\n",
    "        with feedback_out:\n",
    "            feedback_out.clear_output()\n",
    "            print(s)\n",
    "        return\n",
    "\n",
    "    check = widgets.Button(description=\"check\")\n",
    "    check.on_click(check_selection)\n",
    "\n",
    "    hint_out = widgets.Output()\n",
    "\n",
    "    def hint_selection(b):\n",
    "        with hint_out:\n",
    "            print(hint)\n",
    "\n",
    "        with feedback_out:\n",
    "            feedback_out.clear_output()\n",
    "            print(hint)\n",
    "\n",
    "    hintbutton = widgets.Button(description=\"hint\")\n",
    "    hintbutton.on_click(hint_selection)\n",
    "\n",
    "    return widgets.VBox([description_out, alternativ, widgets.HBox([hintbutton, check]), feedback_out], \n",
    "                        layout=Layout(display='flex',\n",
    "                                      flex_flow='column',\n",
    "                                      align_items='stretch',\n",
    "                                      width='auto'))\n",
    "\n",
    "\n",
    "# Pre-defined multiple choices answers for smallbaselineApp_aria.ipynb\n",
    "\n",
    "inv_quality = create_multiple_choice_widget(\n",
    "    '',\n",
    "    ['A', 'B', 'C', 'D'],\n",
    "    'B',\n",
    "    '[hint]: one can indicate both decorrelation noise and possible unwrapping errors.',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "insar",
   "language": "python",
   "name": "insar"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
